import 'package:flutter/material.dart';

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('StatefulWidget'),
        leading: const Icon(Icons.menu),
        centerTitle: true,
        actions: const [
          Icon(Icons.settings),
        ],
      ),
      body:MyState(),
    );
  }
}

class MyState extends StatefulWidget {
  @override
  _MyStateState createState() => _MyStateState();
}

class _MyStateState extends State<MyState> {
  int _num = 0 ;

  void _increment(){
    setState(() {
      _num++;
    });
  }

  void _decrement(){
    setState(() {
      _num--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ShareDataWidget(
      num: _num,
      child: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: _decrement,
              child: Text('-'),
            ),
            Padding(
              padding: EdgeInsets.all(20),
              // child: Text('$_num'),
              //跨组件访问数据
              child: MyCounter(),
            ),
            ElevatedButton(
              onPressed: _increment,
              child: Icon(Icons.add),
            )
          ],
        ),
      ),
    );
  }
}

class MyCounter extends StatefulWidget {
  const MyCounter({Key? key}) : super(key: key);

  @override
  State<MyCounter> createState() => _MyCounterState();
}

class _MyCounterState extends State<MyCounter> {
  @override
  Widget build(BuildContext context) {
    //使用InheritedWidget
    final count = ShareDataWidget.of(context)?.num ?? 0;
    return Text('$count');
  }
}

//数据共享的组件
class ShareDataWidget extends InheritedWidget {
  final int num;
  final Widget child;

  const ShareDataWidget({Key? key, required this.child, required this.num}) : super(key: key, child: child);

  static ShareDataWidget? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<ShareDataWidget>();
  }

  @override
  bool updateShouldNotify(ShareDataWidget oldWidget) {
    return true;
  }
}